<%@ page language="java" contentType="text/html; charset=US-ASCII"
    pageEncoding="US-ASCII"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="java.util.*,org.atf.utils.ATFUtils,org.atf.model.entity.User" %>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="../../favicon.ico">

    <title>ATF Login Page</title>

    <!-- Bootstrap core CSS -->
    <link href="css/bootstrap.css" rel="stylesheet">

    <!-- Custom styles for this template -->
    <link href="css/site.css" rel="stylesheet">

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
    <%
    	String errorMessge="";
    	if(ATFUtils.isTamEnabled()){
    		String userName= request.getHeader(ATFUtils.getAppUserHeader());
    		String groups= request.getHeader(ATFUtils.getAppGroupsHeader());
    		if (userName != null && groups != null) {
    			User user=ATFUtils.buildTamUser(userName, groups);
    			session.setAttribute("ATFUser", user);
    			ATFUtils.addUserLoginAudit(user);
				response.sendRedirect("index.jsp");
    		}else{
    			errorMessge="TAM is enabled but no user/group";
    		}
    	}else{
	    	String username = request.getParameter("username");
	        String password = request.getParameter("password");
		    
			if (username != null && password != null) {
				User user= null;
				if(username.equals("ATFadmin")){
					user=ATFUtils.getATFAdminRoot(username, password);
				}else{
					user=ATFUtils.isUserAuthenticated(username, password);
					
				}
				
				if(user != null){
					session.setAttribute("ATFUser", user);
					ATFUtils.addUserLoginAudit(user);
					response.sendRedirect("index.jsp");
				}else{
					errorMessge="Unabled to Authenticate. Either Username or password in invalid";
				}
				
			}
    	}
    	
	
     %>
  </head>

  <body>

    <div class="container">
	  
      <form class="form-signin"  action="" method="post" role="form">
        <h2 class="form-signin-heading">Application Login</h2>
        <%
	  	if( !errorMessge.contentEquals("")){
	  	
	   %>
	   	<div class="alert alert-danger" role="alert"><%=errorMessge %></div>
	   <%
	   	}
	    %>
        <input type="text" name="username" class="form-control" placeholder="Username" required autofocus>
        <br>
        <input type="password" name="password" class="form-control" placeholder="Password" required>
        <button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
      </form>

    </div> <!-- /container -->
  </body>
</html>
